package com.link_with_health.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;

import java.util.Map;

public class JsonUtil {
    public static String obj2json(Object obj) throws Exception {
        return JSON.toJSONString(obj);
    }

    public static <T> T json2obj(String jsonStr, Class<T> clazz) throws Exception {
        return JSON.parseObject(jsonStr, clazz);
    }

    public static <T> Map<String, Object> json2map(String jsonStr) throws Exception {
        return JSON.parseObject(jsonStr, Map.class);
    }

    public static <T> T map2obj(Map<?, ?> map, Class<T> clazz) throws Exception {
        return JSON.parseObject(JSON.toJSONString(map), clazz);
    }

    public static JSONObject str2JsonObject(String string) throws Exception {
        return JSON.parseObject(string);
    }

    //
    public static <T> HashMap<String, Object> bean2Map (T bean) throws Exception {
        String beanStr = JSON.toJSONString(bean);
        return JSON.parseObject(beanStr,HashMap.class);
    }


}
